'use strict';

var app = angular.module("myApp", ['ui.bootstrap', 'ui.bootstrap.treeview']);

app.controller("manageController2", ['$scope', '$compile', '$sce', function ($scope, $compile, $sce) {
    $scope.query = {
        fault: FaultNode.faults[0]
    };

    $scope.toJSON = function (val) {
        return angular.toJson(val, true);
    };

    // $scope.faults=FaultNode.faults;
    FaultNode.loadFaults.callbacks.push(function () {
        return $scope.faults = FaultNode.faults;
    });
    FaultNode.loadFaults.callbacks.push(function () {
        return $scope.query.fault = FaultNode.faults[0];
    });
    FaultNode.loadFaults();

    $scope.components = ComponentNode.components;
    ComponentNode.loadComponents.callbacks.push(function () {
        return $scope.components = ComponentNode.components;
    });
    ComponentNode.loadComponents();

    $scope.graphTree = new TreeView();
    $scope.$watch('query', function () {
        console.info($scope.query);
        if ($scope.query.fault) Graph.loadGraph($scope.query.fault.id, function (g) {
            $scope.graph = g;
            $scope.graphTree.nodes = [$scope.graph.toJsonTree()];
            $scope.graphTree.collapseAll();
            console.info($scope.graphTree);

            $scope.$apply();
            var queryUrl = 'start n=node(' + $scope.query.fault.id + ') match (n)-[*]->(p) return n,p';
            $scope.faultUrl = $sce.trustAsResourceUrl('../browser/NeoEmbed.html?query=' + encodeURIComponent(queryUrl).replace(/=/g, '%3d'));
        });
    }, true);

    $scope.$watch('graphTree.selectedNode', function () {
        var data = $scope.graphTree.selectedNode;
        if ($scope.graph) {
            var template = $scope.graph.getNode(data.referId).getReadonlyTemplate(data.referEdge, $scope.graph);
            var tp = $compile(template)($scope);
            angular.element("#nodeDetail2").html("").append(tp);
        }
    }, true);

    $scope.displayAllTree = function () {
        var gs = [];
        var gts = FaultNode.faults.length;
        var gtn = 0;
        var _iteratorNormalCompletion = true;
        var _didIteratorError = false;
        var _iteratorError = undefined;

        try {
            for (var _iterator = FaultNode.faults[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
                var f = _step.value;

                Graph.loadGraph(f.id, function (g) {
                    gs.push(g);
                    gtn++;
                    if (gtn == gts) {
                        $scope.graphTree.nodes = gs.map(function (g) {
                            return g.toJsonTree();
                        });
                        $scope.graphTree.collapseAll();
                    }
                });
            }
        } catch (err) {
            _didIteratorError = true;
            _iteratorError = err;
        } finally {
            try {
                if (!_iteratorNormalCompletion && _iterator.return) {
                    _iterator.return();
                }
            } finally {
                if (_didIteratorError) {
                    throw _iteratorError;
                }
            }
        }
    };

    $scope.allowFeature = function (index) {
        return false;
    };

    $scope.allowCause = function (index) {
        return false;
    };

    $scope.allowComponent = function (index) {
        return false;
    };

    $scope.attachNode = function (type, id) {
        return null;
    };

    $scope.attachFeature = function (id) {
        return $scope.attachNode('Feature', id);
    };
    $scope.attachReason = function (id) {
        return $scope.attachNode('Reason', id);
    };
    $scope.attachComponent = function (index) {
        var cp = ComponentNode.components.filter(function (nd) {
            var _iteratorNormalCompletion2 = true;
            var _didIteratorError2 = false;
            var _iteratorError2 = undefined;

            try {
                for (var _iterator2 = $scope.graph.edges[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
                    var eg = _step2.value;

                    if (eg.from == index && eg.to == nd.id) return false;
                }
            } catch (err) {
                _didIteratorError2 = true;
                _iteratorError2 = err;
            } finally {
                try {
                    if (!_iteratorNormalCompletion2 && _iterator2.return) {
                        _iterator2.return();
                    }
                } finally {
                    if (_didIteratorError2) {
                        throw _iteratorError2;
                    }
                }
            }

            return true;
        })[0];
        var edge = new Edge($scope.graph, index, cp.id);
        $scope.graph.addNode(cp);
        $scope.graph.edges.push(edge);
    };

    $scope.attachGroup = function (index, groupType) {
        return $scope.attachNode(groupType, index);
    };

    $scope.appendArgv = function (index) {
        $scope.graph.getNode(index).params.push(0);
    };

    $scope.removeArgv = function (index, position) {
        $scope.graph.getNode(index).params.splice(position, 1);
    };

    $scope.removeEdge = function (edge) {
        return null;
    };
}]);
